#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Writer.Embedding
{
    /// <summary>
    /// The CrossReference class represents a single item in the cross reference table of the source PDF document.
    /// It consists of an object reference and a location within the source document.
    /// </summary>
    internal class CrossReference
    {
        // Private fields
        // ==============
        #region Private fields
        private CrossReferenceType _type;
        private PdfObjectReference _objectReference;
        private long _location;
        private int _parentIndex;
        private PdfObjectReference _parentReference;
        #endregion



        // Base class overrides
        // ====================
        #region Object overrides
        /// <summary>
        /// Indicates if this instance is equal to the specified instance.
        /// If the other instance is a PdfObjectReference then it will be
        /// compared to this instance's reference.
        /// Otherwise, if the other instance is a cross-reference then the
        /// two instances will be compared based on their type and content.
        /// </summary>
        /// <param name="obj">The other instance</param>
        /// <returns>A boolean value indicating that the two instances are equal, or not.</returns>
        public override bool Equals(object obj)
        {
            if (obj is CrossReference)
                return Equals((CrossReference)obj);

            if (obj is PdfObjectReference)
                return Equals((PdfObjectReference)obj);

            return false;
        }

        /// <summary>
        /// Retrieves the hash code for this cross-reference. The hash value will be that
        /// of the cross-reference's object reference.
        /// </summary>
        /// <returns>The hash code</returns>
        public override int GetHashCode()
        {
            return this.ObjectReference.GetHashCode();
        }
        /// <summary>
        /// Returns the string representation of this cross-reference.
        /// </summary>
        /// <returns>The string</returns>
        public override string ToString()
        {
            string result = null;

            switch (this.Type)
            {
                case CrossReferenceType.FileOffset:
                    result = String.Format(
                        "{0}, at file offset {1}", 
                        this.ObjectReference, 
                        this.Location);
                    break;

                case CrossReferenceType.ObjectStream:
                    result = String.Format(
                        "{0}, in object {1}, index {2}", 
                        this.ObjectReference, 
                        this.ParentReference, 
                        this.ParentIndex);
                    break;
            }

            return result;
        }
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Retrieves the location within the source document of the object that this cross reference entry refers to.
        /// </summary>
        public long Location
        {
            get { return _location; }
        }

        /// <summary>
        /// Retrieves the object reference for this cross reference entry.
        /// </summary>
        public PdfObjectReference ObjectReference
        {
            get { return _objectReference; }
        }

        /// <summary>
        /// Sets or retrieves the index of this reference within the parent object's stream
        /// (for stream object cross-references.)
        /// </summary>
        public int ParentIndex
        {
            get { return _parentIndex; }
            set { _parentIndex = value; }
        }

        /// <summary>
        /// Sets or retrieves the object reference of the parent object for this reference
        /// (for stream object cross-references.)
        /// </summary>
        public PdfObjectReference ParentReference
        {
            get { return _parentReference; }
            set { _parentReference = value; }
        }

        /// <summary>
        /// Sets or retrieves the type of this cross-reference.
        /// </summary>
        public CrossReferenceType Type
        {
            get { return _type; }
            set { _type = value; }
        }
        #endregion


        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Indicates if the two cross-references are equal.
        /// </summary>
        /// <param name="crossReference">The other instance</param>
        /// <returns>A boolean value indicating that the two instances are equal, or not.</returns>
        public bool Equals(CrossReference crossReference)
        {
            bool result = false;

            if (crossReference.Type == this.Type)
            {
                switch (this.Type)
                {
                    case CrossReferenceType.FileOffset:
                        result = this.Location == crossReference.Location;
                        break;

                    case CrossReferenceType.ObjectStream:
                        result =
                            this.ParentReference.Equals(crossReference.ParentReference) &&
                            this.ParentIndex.Equals(crossReference.ParentIndex);
                        break;
                }
            }

            return result;
        } // Equals()

        /// <summary>
        /// Indicates if the this cross-reference matches the specified object reference.
        /// </summary>
        /// <param name="obj">The object reference</param>
        /// <returns>A boolean value indicating that the two instances are equal, or not.</returns>
        public bool Equals(PdfObjectReference obj)
        {
            return this.ObjectReference.Equals(obj);
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new CrossReference instance.
        /// </summary>
        /// <param name="reference">The object reference</param>
        /// <param name="location">The location within the document of this object</param>
        public CrossReference(PdfObjectReference reference, long location)
        {
            if (null == reference) throw new ArgumentNullException("reference");

            _type = CrossReferenceType.FileOffset;
            _objectReference = reference;
            _location = location;
        }

        /// <summary>
        /// Constructs a new CrossReference instance for an object that is located inside an object stream.
        /// </summary>
        /// <param name="reference">The object reference</param>
        /// <param name="parent">The containing object of this reference.</param>
        /// <param name="index">The index within the parent object.</param>
        public CrossReference(PdfObjectReference reference, PdfObjectReference parent, int index)
        {
            if (null == reference) throw new ArgumentNullException("reference");

            _type = CrossReferenceType.ObjectStream;
            _objectReference = reference;
            _parentReference = parent;
            _parentIndex = index;
        }

        #endregion

    } // CrossReference class
}
